<style scoped>
    .layui-form >>> .layui-form-select{
        z-index: inherit;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加数据类别</legend>
        </fieldset>

        <form class="layui-form " >
            <input type="hidden" name="guid" placeholder="" autocomplete="off" :value="guid" class="layui-input">
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name"  placeholder="请输入名称" :value="datacategory.name" required lay-verify="required"   autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">编号</label>
                <div class="layui-input-block"  style="width: 300px" >
                    <select   id="code" name="upGuid"  lay-filter="code" disabled   required   lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
                <div class="layui-input-block"  style="width: 200px; position: absolute;left: 470px;top: 179px;" >
                    <select   id="isSub"  name="isSub"  lay-filter="isSub"  required    lay-search="" >
                        <option value="one">一级</option>
                        <option value="true">下级</option>
<!--                        <option value="sub">本级</option>-->

                    </select>
                </div>


            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属大类</label>
                <div class="layui-input-block">
                    <select   id="type"  name="type"  lay-filter="type"  required  lay-verify="required"    lay-search="">
                        <option value="">请选择</option>
                    </select>

                </div>
            </div>
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">状态</label>-->
<!--                <div class="layui-input-block">-->

<!--                    <select   id="status"  name="status"  lay-filter="status"  required  lay-search="">-->
<!--                        <option value="">请选择</option>-->
<!--                        <option value="1" v-if="datacategory.status==1" selected="selected" >启用</option>-->
<!--                        <option value="1" v-else  >启用</option>-->
<!--                        <option  v-if="datacategory.status==2" selected="selected" value="2">禁用</option>-->
<!--                        <option  v-else value="2">禁用</option>-->
<!--                    </select>-->

<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">成本收入</label>
                <div class="layui-input-block">
                       <select   id="costincome"  name="costincome" required lay-verify="required"    lay-filter="costincome"    lay-search="">
                        <option value="">请选择</option>
                    </select>

                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">说明</label>
                <div class="layui-input-block">
                    <textarea name="remark" :value="datacategory.remark" placeholder="请输入备注内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'EditOrganization',
        props: ['id'],
        data () {
            return {
                 guid : this.id,
                 adata: {},
                datacategory:{},
                typeEmnu:[],
                costincomeEmnu:[]
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                let loader = layer.load();
                //获取回显数据
                $.ajax({
                    url: '/produce/Datacategory!getBase.action',
                    data: {guid: that.guid},
                    type: "post",
                    dataType: "json",
                    success: function(res) {

                        if (res.info != "error") {
                            //加载类型枚举值
                            $.each(res.info.typeEmnu, function (index, item) {
                                    $('#type').append("<option   value=" +item.dictkey +  " > " + item.dicttext  + "</option>" );
                            });


                            //加载类型枚举值
                            $.each(res.info.costincomeEmnu, function (index, item) {
                                    $('#costincome').append("<option   value=" +item.dictkey +  " > " + item.dicttext  + "</option>" );
                            });

                            //加载类型枚举值
                            $.each(res.info.datacategoryEmnu, function (index, item) {
                                    $('#code').append("<option   value=" +item.guid +  " > " + item.name  + "(" +item.code  +") </option>" );
                            });


                            form.render('select');
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });

                form.on('select(isSub)', function(data){
                    var isSub =  data.value;
                     if(isSub == "one" ){
                         $("#code").val("");
                         $("#code").attr("disabled","disabled")
                     }else{
                         $("#code").removeAttr("disabled");
                     }
                    form.render('select');
                });


                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    var subValue = $("#isSub").val();
                    var codeValue  = $("#code").val();

                    if(subValue == 'true' && (codeValue==''|| codeValue==null)){
                        that.toast("请选择编号");
                        return false;
                    }





                    let loader = layer.load();
                    $.ajax({
                        url: '/produce/Datacategory!add.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "success") {
                                that.toast("添加成功");
                                that.$parent.switchPage('ListDatacategory');
                            } else if (res.info == 'noDel') {
                                that.toast("有子数据类别，不能进行修改删除！");
                            }else{
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });

        },
        methods: {
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            }
        }
    }
</script>
